import ArrayPrototype from './ArrayPrototype.js'

class Observe {
  constructor(data) {
    this.walk(data)
  }

  walk(data) {
    if (typeof data != 'object' || data == null) return
    if (Array.isArray(data)) {
      Object.setPrototypeOf(data, ArrayPrototype)
    }
    Object.keys(data).forEach((key) => {
      this.defineReactive(data, key, data[key])
    })
  }

  defineReactive(obj, key, value) {
    this.walk(value)
    Object.defineProperty(obj, key, {
      //读取对象属性
      get() {
        return value
      },
      //设置对象属性
      set: (newValue) => {
        this.walk(newValue)
        value = newValue
      },
    })
  }
}

export default Observe
